package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.GoodsOrderConditionForm;
import office.action.business.form.GoodsOrderForm;
import office.action.business.wrapper.GoodsOrderWrapper;
import office.bo.business.impl.Contract;
import office.bo.business.impl.GoodsOrder;
import office.bo.business.impl.GoodsOrderDetail;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IGoodsOrderService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class GoodsOrderServiceImpl extends BaseBizServiceImpl implements IGoodsOrderService{
	
	public void editGoodsOrder(GoodsOrderForm form,List<GoodsOrderDetail> goodsOrderDetailList)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			GoodsOrder bo = (GoodsOrder)domainProviderSetImpl.getGoodsOrderDP().newEmptyObj();
			bo =(GoodsOrder) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
			//保存订单铲平明细
			if(null!=goodsOrderDetailList){
				for (int i = 0; i < goodsOrderDetailList.size(); i++) {
					GoodsOrderDetail goodsOrderDetail=goodsOrderDetailList.get(i);
					if(null!=goodsOrderDetail){
						goodsOrderDetail.generatorUUID();
						goodsOrderDetail.setGoodsOrderId(bo.getId().toString());
						goodsOrderDetail.setProductType(bo.getOrderType());
						goodsOrderDetail.store(domainProviderSetImpl);
					}
				}
			}
			//修改合同表中的状态，表示已经备货
			Contract contract = (Contract)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractDP(), bo.getContractId());
			if("1".equals(bo.getOrderType())){
				contract.setIsStockUp("1");
			}else{
				contract.setIsDelivery("1");
			}
			contract.store(domainProviderSetImpl);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑订单明细失败!", e);
		}
	}
	
	public Page getPageInfo(GoodsOrderConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getGoodsOrderDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "GoodsOrder.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getCustomerName())){
				condMap.put("customerName", condition.getCustomerName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findGoodsOrderByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<GoodsOrderWrapper> findAllGoodsOrder(GoodsOrderConditionForm condition,Page page)throws BusinessException{
		List<GoodsOrderWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getGoodsOrderDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getCustomerName())){
				condMap.put("customerName", condition.getCustomerName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findGoodsOrderByIdAndName",condMap,new HashMap(),page);
			GoodsOrderWrapper wrapper;
			for(int i=0;i<list.size();i++){
				GoodsOrder bo = (GoodsOrder)list.get(i);
				wrapper = new GoodsOrderWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取订单明细列表失败！", e);
		}
		return wrapperList;
	}

	public void delGoodsOrderById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getGoodsOrderDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除订单明细失败！", e);
		}
	}
	
	public GoodsOrderForm getById(String id)throws BusinessException{
		GoodsOrderForm form = new GoodsOrderForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			GoodsOrder bo = (GoodsOrder)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getGoodsOrderDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取订单明细失败!", e);
		}
		return form;
	}
	/**
	 * 根据标书的ID获取标书对应的产品明细
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public List<GoodsOrderWrapper> findAllGoodsOrderByContractId(GoodsOrderConditionForm condition)throws BusinessException{
		List<GoodsOrderWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getGoodsOrderDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition)){
				condMap.put("contractId", condition.getContractId());
			}
			if(null!=condition&&!Util.isNull(condition)){
				condMap.put("orderType", condition.getOrderType());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findGoodsOrderByContractId",condMap,new HashMap());
			GoodsOrderWrapper wrapper;
			for(int i=0;i<list.size();i++){
				GoodsOrder bo = (GoodsOrder)list.get(i);
				wrapper = new GoodsOrderWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据合同获取订单列表失败！", e);
		}
		return wrapperList;
	}
}
